package com.yiwei.jmx.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.DealApplyDetailBean;
import com.yiwei.jmx.api.bean.RepayHuaKouBean;
import com.yiwei.jmx.api.model.BankCardListModel;
import com.yiwei.jmx.api.model.DealApplyDetailModel;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Dialog;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.Spans;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.BankSelectDialog;
import com.yiwei.jmx.view.MyProgressView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class RepayMoneyActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.progress_view)
    MyProgressView progressView;
    @BindView(R.id.txt_progress_tips)
    TextView txtProgressTips;
    @BindView(R.id.txt_tips_one)
    TextView txtTipsOne;
    @BindView(R.id.txt_tips_two)
    TextView txtTipsTwo;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_borrow_date)
    TextView txtBorrowDate;
    @BindView(R.id.txt_repay_date)
    TextView txtRepayDate;
    @BindView(R.id.txt_repay_fee)
    TextView txtRepayFee;
    @BindView(R.id.txt_total_money)
    TextView txtTotalMoney;
    @BindView(R.id.txt_bank)
    TextView txtBank;
    @BindView(R.id.layout_bank)
    LinearLayout layoutBank;
    @BindView(R.id.txt_total_money_sec)
    TextView txtTotalMoneySec;
    @BindView(R.id.btn_repay)
    SuperButton btnRepay;
    @BindView(R.id.btn_sao)
    TextView btnSao;
    @BindView(R.id.yuqi_progress_view)
    MyProgressView yuqiProgressView;
    @BindView(R.id.img_bank_icon)
    ImageView imgBankIcon;
    private Unbinder unbiner;
    private String deal_id;
    private String bank_id;
    private BankSelectDialog bankSelectDialog;
    private DealApplyDetailModel.DataBean totalData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_money);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        bankSelectDialog = new BankSelectDialog(this);
        bankSelectDialog.setOnSelect(new BankSelectDialog.OnSureListener() {
            @Override
            public void onSure(BankCardListModel.DataBean item) {
                bank_id = item.id;
                Glide.with(RepayMoneyActivity.this).load(item.icon).into(imgBankIcon);

                txtBank.setText("(" + item.bank_name + item.last_num + ")");
            }
        });

        deal_id = getIntent().getStringExtra("deal_id");
        getDealApply(deal_id);
    }

    private void getDealApply(String deal_id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        DealApplyDetailBean bean = new DealApplyDetailBean();
        bean.deal_id = deal_id;
        Api.api_service.getDealApplyDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    totalData = result.data;
                    DealApplyDetailModel.DataBean item = result.data;
                    txtBorrowDate.setText("借款日期\n" + item.create_date);
                    txtRepayDate.setText("还款日期\n" + item.repay_date);
                    txtRepayFee.setText("逾期费用\n￥" + item.impose_fee);
                    txtMoney.setText(item.true_money);
                    txtTotalMoney.setText("￥" + item.true_money);

                    if (Float.valueOf(result.data.break_day) > 0) {
                        yuqiProgressView.setVisibility(View.VISIBLE);
                        txtProgressTips.setTextColor(0xffFF2740);
                        txtTipsOne.setTextColor(0xffFF2740);
                        txtTipsTwo.setTextColor(0xffFF2740);
                        txtMoney.setTextColor(0xffFF2740);
                        txtRepayFee.setTextColor(0xffFF2740);
                        txtTipsOne.setText("逾期天数");
                        txtProgressTips.setText(item.break_day);
                    } else {
                        progressView.setVisibility(View.VISIBLE);
                        progressView.setProgressSec(Float.valueOf(item.last_day), Float.valueOf(item.day));
                        txtTipsOne.setTextColor(0xff10BC69);
                        txtTipsTwo.setTextColor(0xff566F8E);
                        txtMoney.setTextColor(0xff0072FF);
                        txtTipsOne.setText("还款倒计时");
                        txtProgressTips.setText(item.last_day);
                    }

                    Spanned txt_one = Spans.with(this).font("共计", getResources().getDimensionPixelSize(R.dimen.x24), 0xff566F8E).
                            font(item.true_money, getResources().getDimensionPixelSize(R.dimen.x48), 0xffFF0018).style(Typeface.BOLD).
                            font("元", getResources().getDimensionPixelSize(R.dimen.x24), 0xff566F8E).
                            build();
                    txtTotalMoneySec.setText(txt_one);

                });
    }


    public void getHuaKou(String deal_id, String bankcard_id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        RepayHuaKouBean bean = new RepayHuaKouBean();
        bean.deal_id = deal_id;
        bean.bankcard_id = bankcard_id;
        Api.api_service.getRepayHuaKou(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    // -1 - 无还款信息 0 - 还款失败、1 - 还款成功、2 - 还款审核中
                    switch (result.data.repay_staus) {
                        case "-1":

                            break;
                        case "0":
                            Dialog.showRepayFail(this, new Dialog.DialogDefaultClickListener() {
                                @Override
                                public void confirm() {
                                    finish();
                                }

                                @Override
                                public void cancel() {

                                }
                            });
                            break;
                        case "1":
                            Dialog.showRepaySuccess(this, new Dialog.DialogDefaultClickListener() {
                                @Override
                                public void confirm() {
                                    finish();
                                }

                                @Override
                                public void cancel() {

                                }
                            });
                        case "2":
                            Intent intent = new Intent(this, RepayStatusActivity.class);
                            intent.putExtra("deal_id", deal_id);
                            intent.putExtra("need_money", totalData.true_money);
                            startActivity(intent);
                            overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                            finish();
                            break;
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_right, R.id.layout_bank, R.id.btn_repay, R.id.btn_sao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Util.startActivity(this, MyBorrowRecordActivity.class);
                break;
            case R.id.layout_bank:
                bankSelectDialog.show();
                break;
            case R.id.btn_repay:
                getHuaKou(deal_id, bank_id);
                break;
            case R.id.btn_sao:
                if (totalData == null) {
                    getDealApply(deal_id);
                    return;
                }
                Intent intent = new Intent(this, RepayQrcodeActivity.class);
                intent.putExtra("action", "REPAY");
                intent.putExtra("deal_id", deal_id);
                intent.putExtra("pay_type", "1");
                intent.putExtra("money", totalData.true_money);
                intent.putExtra("yuqi", totalData.impose_fee);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
//                Util.startActivity(this, RepayQrcodeActivity.class);
                break;
        }
    }
}
